// pages/sendToHome/sendToHome.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        defaultBarHeight:20,
        // 开始滑动位置
        startX:0,
        // 菜单选择
        tbox:0,
        // 菜的总数量
        totalFoodNum:0,
        // 菜单选择
        meanueIndex:0,
        typeToLeft:true,
        // 屏幕高度
        screenHeight:0,
        //口袋选择
        radioCheck:0,
        showCarDel:false,
        // packetList:["口袋1","口袋2"],
        packetList:[
            {
                packetName:"口袋1",
                list:[],
                price:0,
                num:0
            },
            {
                packetName:"口袋2",
                list:[],
                price:0,
                num:0
            }
        ],
        packetNowIndex:0,
        foodList:[
            {
                name:'香卤鸡爪',
                img:'../../images/sendToHome/food1.png',
                num:'30g/份',
                isSc:false,
                sale:565,
                hpd:'100%',
                price:2.7,
                shoeDel:false,
                // 菜的数量
                fnum:0,
                shoeSc:false
            },
            {
                name:'香卤鸡爪2',
                img:'../../images/sendToHome/food1.png',
                num:'30g/份',
                isSc:false,
                sale:565,
                hpd:'100%',
                price:2.7,
                shoeDel:false,
                // 菜的数量
                fnum:0,
                shoeSc:false
            },
            {
                name:'香卤鸡爪3',
                img:'../../images/sendToHome/food1.png',
                num:'30g/份',
                isSc:false,
                sale:565,
                hpd:'100%',
                price:2.7,
                shoeDel:false,
                // 菜的数量
                fnum:0,
                shoeSc:false
            }
        ],
        showCarDetail:false,
        scroll:true
    },
    toIndex(e){
        wx.switchTab({
          url: '../index/index',
        })
    },
    toConfirm(events){
        wx.navigateTo({
          url: '../confirmOrder/confirmOrder',
          events: events,
          success: (result) => {},
          fail: (res) => {},
          complete: (res) => {},
        })
    },
    // 展示购物车细节
    showShoppingCar(e){
        if(this.data.showCarDetail == false){
            this.setData({
                showCarDetail:true
            })
        }else{
            this.setData({
                showCarDetail:false
            })
        }
    },
    //口袋添加
    packetAdd(e){
        var packetNmae = "";
        packetNmae = "口袋" + (this.data.packetList.length + 1).toString();
        var packet = {
            packetName:packetNmae,
            list:[],
            price:0,
            num:0
        }
        // console.log(e)
        this.data.packetList.push(packet);
        var list = this.data.packetList;
        this.setData({
            packetList:list
        })
        console.log(this.data.packetList)
    },
    //口袋选择，外送到家页面
    packetClick(e){
        // console.log(e)
        this.setData({
            packetNowIndex:e.currentTarget.dataset.pindex
        })
    },
    //口袋选择
    radioCheckChange(e){
        this.setData({
            radioCheck:e.currentTarget.dataset.index,
            packetNowIndex:e.currentTarget.dataset.index
        })
        if(this.data.radioCheck == this.data.packetList.length-1){
            this.packetAdd();
        }
    },
    clearPacket(e){
        this.data.packetList[e.currentTarget.dataset.index].list = []
        var list = this.data.packetList;
        this.setData({
            packetList:list
        })
    },
    //转到单品详情
    toDetail(events){
        wx.navigateTo({
          url: '../foodDetail/foodDetail',
          events: events,
          success: (result) => {},
          fail: (res) => {},
          complete: (res) => {},
        })
    },
    // 菜单选择
    mCheck(e){
        var index = e.currentTarget.dataset.index;
        this.setData({
            meanueIndex:index
        })
        // console.log(this.data.meanueIndex)
    },
    // 菜的数量加
    numAdd(e){
        this.data.foodList[e.currentTarget.dataset.index].fnum = this.data.foodList[e.currentTarget.dataset.index].fnum + 1
        var list = this.data.foodList;
        this.setData({
            foodList:list
        })
        // console.log(this.data.foodList)
        // 计算总数
        var total = 0
        for(var i=0;i<this.data.foodList.length;i++){
            total += this.data.foodList[i].fnum;
        }
        this.setData({
            totalFoodNum:total
        })
        // 计算口袋价格
        // this.data.packetList[this.data.packetNowIndex].list[e.currentTarget.dataset.index].fnum += 1
        var totalprice = 0
        for(var i=0;i<this.data.packetList[this.data.packetNowIndex].list.length;i++){
            totalprice += this.data.packetList[this.data.packetNowIndex].list[i].price * this.data.packetList[this.data.packetNowIndex].list[i].fnum;
        }
        this.data.packetList[this.data.packetNowIndex].price = totalprice;
        console.log(this.data.packetList)
        this.pnum(this.data.packetNowIndex)
        var nowPlist = this.data.packetList;
        this.setData({
            packetList:nowPlist
        })
    },
    //每个口袋菜的数量
    pnum(k){
        var p = 0;
        for(var i=0;i<this.data.packetList[k].list.length;i++){
            p = p+this.data.packetList[k].list[i].fnum
            console.log(this.data.packetList[k].list[i].fnum)
        }
        this.data.packetList[k].num = p
        // console.log(this.data.packetList[k])
        var nowPlist = this.data.packetList;
        this.setData({
            packetList:nowPlist
        })
    },
    // 菜的数量减
    numCut(e){
        if(this.data.foodList[e.currentTarget.dataset.index].fnum != 0){
            this.data.foodList[e.currentTarget.dataset.index].fnum = this.data.foodList[e.currentTarget.dataset.index].fnum - 1
            var list = this.data.foodList;
            this.setData({
                foodList:list
            })
        }
        // console.log(this.data.foodList)
        // 计算总数
        var total = 0
        for(var i=0;i<this.data.foodList.length;i++){
            total += this.data.foodList[i].fnum;
        }
        this.setData({
            totalFoodNum:total
        })
        // 计算口袋价格
    },
    // 是否收藏
    isSc(e){
        var list;
        if(this.data.foodList[e.currentTarget.dataset.index].isSc == false){
            this.data.foodList[e.currentTarget.dataset.index].isSc = true;
            list = this.data.foodList;
            // console.log(this.data.isSC)
        }else{
            this.data.foodList[e.currentTarget.dataset.index].isSc = false;
            list = this.data.foodList;
            // console.log(this.data.isSC)
        }
        this.setData({
            foodList:list
        })
    },
    /**
     * 记录滑动开始位置
     * @param {*} e 
     */
    tStart(e){
        // console.log('start',e.changedTouches[0].pageX)
        this.setData({
            startX:Number(e.changedTouches[0].clientX)
        })
    },
    /**
     * 计算滑动距离
     * @param {*} e 
     */
    tEnd(e){
        // console.log('end',e)
        var end = Number(e.changedTouches[0].clientX)
        var distance = end-this.data.startX;
        var list;
        if(e.currentTarget.dataset.type == "meanu"){
            if(distance < 0){
                this.setData({
                    typeToLeft:false
                })
            }
            if(distance > 0){
                this.setData({
                    typeToLeft:true
                })
            }
        }
        if(e.currentTarget.dataset.type == "foodList"){
            // 是否显示删除按钮
            if(distance < -100){
                this.data.foodList[e.currentTarget.dataset.index].shoeDel = true;
                this.data.foodList[e.currentTarget.dataset.index].shoeSc = false;
                list = this.data.foodList
                this.setData({
                    foodList:list
                })
            }else if(distance > 100){
                this.data.foodList[e.currentTarget.dataset.index].shoeDel = false;
                this.data.foodList[e.currentTarget.dataset.index].shoeSc = true;
                list = this.data.foodList
                this.setData({
                    foodList:list
                })
            }else{
                this.data.foodList[e.currentTarget.dataset.index].shoeDel = false;
                this.data.foodList[e.currentTarget.dataset.index].shoeSc = false;
                list = this.data.foodList
                this.setData({
                    foodList:list
                })
            }
        }
        if(e.currentTarget.dataset.type == "car"){
            if(distance < 0){
                this.setData({
                    showCarDel:true
                })
            }else{
                this.setData({
                    showCarDel:false
                })
            }
        }
        this.setData({
            startX:0
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.getSystemInfo({
          success: (result) => {
            //   console.log(result.screenHeight)
            this.setData({
                screenHeight:result.screenHeight
            })
          },
        })
        wx.getSystemInfo({
            success: res => {
                console.log(res)
                let statusBarHeight = res.statusBarHeight;
                this.data.defaultBarHeight = statusBarHeight
                // console.log(statusBarHeight)
            },
            fail(err) {
              console.log(err);
            }
          })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        for(var i=0;i<this.data.packetList.length;i++){
            this.data.packetList[i].list = this.data.foodList
        }
        var list = this.data.packetList;
        this.setData({
            packetList:list
        })
        // console.log(this.data.packetList)
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    /**
   * 页面滚动距离
   */
  onPageScroll: function (e) {
    // console.log(e.scrollTop)
    if(e.scrollTop >= 108){
        this.setData({
            scroll:false
        })
    }else{
        this.setData({
            scroll:true
        })
    }
  }
})